package com.shenchuangit.rydemo.web.personal.controller;

import com.ruoyi.activiti.dto.WorkflowTaskDTO;
import com.ruoyi.activiti.utils.WorkflowHelper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysNoticeService;
import com.shenchuangit.rydemo.web.personal.service.IHomeService;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 首页 Controller
 *
 * @author lbc
 * @date 2020-09-01
 */
@RestController
@RequestMapping("/personal")
public class PersonalAdminController extends BaseController {
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private WorkflowHelper workflowHelper;
    @Autowired
    private IHomeService homeService;

    /**
     * 查询当前登录人角色
     */
    @GetMapping("/getLoginUser")
    public Map<String, String> getLoginUser() {
        Map<String, String> topStringMap = new HashMap<>();
        //权限相关字段赋值
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        String roleIds = "";
        if (CollectionUtils.isNotEmpty(loginUser.getRoles())) {
            for (SysRole role : loginUser.getRoles()) {
                roleIds += "," + role.getRoleId() + ";";
            }
        }
        topStringMap.put("roleIds", roleIds);
        return topStringMap;
    }

    /**
     * 获取个人待办业务
     * @return 待办 列表
     */
    @GetMapping("/getSelfWorkflowTask")
    public TableDataInfo getSelfWorkflowTask(PageDomain pageDomain) {
        startPage();

        TaskQuery taskQuery = this.workflowHelper.initTaskQuery();
        Page<WorkflowTaskDTO> selfTaskPage = this.workflowHelper.getSelfTaskPage(taskQuery, pageDomain);
        // 按日期倒序
        List<WorkflowTaskDTO> list = selfTaskPage.getContent().stream().sorted(Comparator.comparing(WorkflowTaskDTO::getCreateTime).reversed()).collect(Collectors.toList());
        return getDataTable(list);
    }

}
